#include "h_cc2640r2f_pwm_simple.h"

#include <ti/drivers/PWM.h>
#include <ti/drivers/pwm/PWMTimerCC26XX.h>

#include <board.h>

#include "h_cc2640r2f_define.h"




static PWM_Handle H_CC2640R2F_PWMHandle0 = NULL;

signed char H_CC2640R2F_PWM0_Init(unsigned short freq)
{
    PWM_Params H_CC2640R2F_PWMparams;
    
    PWM_init();
    PWM_Params_init(&H_CC2640R2F_PWMparams);
    
    H_CC2640R2F_PWMparams.periodUnits = PWM_PERIOD_HZ;
    H_CC2640R2F_PWMparams.periodValue = freq;
    H_CC2640R2F_PWMparams.dutyUnits = PWM_DUTY_FRACTION;
    H_CC2640R2F_PWMparams.dutyValue = PWM_DUTY_FRACTION_MAX / 2;
    H_CC2640R2F_PWMparams.idleLevel = PWM_IDLE_LOW;
    H_CC2640R2F_PWMparams.custom = NULL;
    
    
    H_CC2640R2F_PWMHandle0 = PWM_open(CC2640R2_LAUNCHXL_PWM0, \
                                        &H_CC2640R2F_PWMparams);
    
    if(H_CC2640R2F_PWMHandle0 == NULL)
    {
        return (-1);
    }

    return 0;
}

/*
static unsigned int h_cc2640r2f_pwm_getrealdutuvalue(unsigned char value)
{
        
	float tf = 0.0f;
        //value = (value < 0)? 0 : value;
        value = (value > 100)? 100 : value;
        
        tf = value / 100.0;
	tf *= PWM_DUTY_FRACTION_MAX;
	return (unsigned int)tf;
}
*/

void H_CC2640R2F_PWM0_StartChannel(void)
{
    PWM_start(H_CC2640R2F_PWMHandle0);
}

void H_CC2640R2F_PWM0_StopChannel(void)
{
    PWM_stop(H_CC2640R2F_PWMHandle0);
}

/*
void H_CC2640R2F_PWM0_ChannelSetDuty(unsigned char duty)
{        
    PWM_setDuty(H_CC2640R2F_PWMHandle0, \
                        h_cc2640r2f_pwm_getrealdutuvalue(duty));
}
*/

signed char H_CC2640R2F_PWM0_ChannelSet_Per(unsigned short per)
{
    return PWM_setPeriod(H_CC2640R2F_PWMHandle0, per);
}

//#include "h_cc2640r2f_simpletime.h"
//
//void H_CC2640R2F_PWM0_Test(void)
//{
//    H_CC2640R2F_PWM0_Init(1024);
//    //PWM_start(H_CC2640R2F_PWMHandle0);
//
//    /*
//    PWM_setPeriod(H_CC2640R2F_PWMHandle0, 294);
//    PWM_start(H_CC2640R2F_PWMHandle0);
//    H_CC2640R2F_SimpleTime_Delay(2000);
//
//    PWM_setPeriod(H_CC2640R2F_PWMHandle0, 330);
//    PWM_start(H_CC2640R2F_PWMHandle0);
//    H_CC2640R2F_SimpleTime_Delay(2000);
//
//    PWM_setPeriod(H_CC2640R2F_PWMHandle0, 350);
//    PWM_start(H_CC2640R2F_PWMHandle0);
//    H_CC2640R2F_SimpleTime_Delay(2000);
//
//    PWM_setPeriod(H_CC2640R2F_PWMHandle0, 393);
//    PWM_start(H_CC2640R2F_PWMHandle0);
//    H_CC2640R2F_SimpleTime_Delay(2000);
//
//    PWM_setPeriod(H_CC2640R2F_PWMHandle0, 441);
//    PWM_start(H_CC2640R2F_PWMHandle0);
//    H_CC2640R2F_SimpleTime_Delay(2000);
//
//    PWM_setPeriod(H_CC2640R2F_PWMHandle0, 495);
//    PWM_start(H_CC2640R2F_PWMHandle0);
//    H_CC2640R2F_SimpleTime_Delay(2000);
//
//    PWM_setPeriod(H_CC2640R2F_PWMHandle0, 556);
//    PWM_start(H_CC2640R2F_PWMHandle0);
//    H_CC2640R2F_SimpleTime_Delay(2000);
//*/
//}

